import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qaun_ni_dev/utils/date_time.dart';
import 'package:qaun_ni_dev/pages/adminManage/admin_update.dart';
import 'package:qaun_ni_dev/provider/admin_provide.dart';

class AdminDetailPage extends StatelessWidget {
  var adminData;
  AdminDetailPage(this.adminData);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('管理员详情'),
        ),
        body: Stack(
          children: <Widget>[
            _detailContent(),
            _optionBtn(context, adminData),
          ],
        ));
  }

  Widget _detailContent() {
    return Container(
        padding: EdgeInsets.only(top: 10, left: 10, right: 10, bottom: 80),
        height: 610,
        child: ListView(
          children: <Widget>[
            Column(
              children: <Widget>[
                Container(
                  padding:
                      EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('用户id:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${adminData.id}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                      EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('用户账号:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${adminData.userAccount}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                      EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('用户备注:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${adminData.remark}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                      EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('创建时间:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${timeStampChange(adminData.createTime)}'),
                      )
                    ],
                  ),
                ),
              ],
            )
          ],
        ));
  }

  Widget _optionBtn(context, adminData) {
    return Positioned(
      bottom: 0,
      width: 360,
      height: 80,
      left: 40,
      child: Container(
        alignment: Alignment.center,
        child: Row(
          children: <Widget>[
            Container(
              width: 100,
              height: 40,
              padding: EdgeInsets.only(top: 10, bottom: 10),
              margin: EdgeInsets.only(right: 80),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(40)),
                color: Colors.lightBlue,
              ),
              child: InkWell(
                onTap: () {
                  print('编辑');
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return AdminUpdate(adminData);
                  }));
                },
                child: Text(
                  '编辑',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
            Container(
              width: 100,
              height: 40,
              padding: EdgeInsets.only(top: 10, bottom: 10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(40)),
                color: Colors.redAccent,
              ),
              child: InkWell(
                onTap: () {
                  _deleteAdmin(context, adminData);
                },
                child: Text(
                  '删除',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _deleteAdmin(context, adminData) {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('警告'),
          content: Text('确认要删除该条数据吗？'),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () {
                  Navigator.of(context).pop();
                } , //关闭对话框
            ),
            FlatButton(
              child: Text("确认"),
              onPressed: () {
                Provider.of<AdminProvide>(context,listen: false).deleteAdminInfo(context,adminData);
                Navigator.of(context).pop(); //关闭对话框
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }
}
